﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Defect_Tracking_System.Helpers;

namespace Defect_Tracking_System.Models
{
    public class ProjectMemberModel
    {
        ProjectManagement projectManager = ProjectManagement.GetInstance();
        public List<MemberAccount> ProjectMembers;
        public List<MemberAccount> GroupMembers;

        public int projectMemberPageIndex;
        public int groupMembersPageIndex;
        public int totalProjectMemberPage;
        public int totalGroupAccountPage;
        public string projectId;
        public string keyWord = "Search Member";


        public ProjectMemberModel(string ProjectId, int projectMemberPage, int accountPage)
        {
            projectId = ProjectId;

            var projectMember = projectManager.GetProjectMembers(ProjectId).OrderBy(m => m.Account.UserName);
            var groupMember = projectManager.GetGroupMembersExceptProjectMembers(ProjectId).OrderBy(m => m.UserName);

            PaginatedList<ProjectMember> _projectMembers = new PaginatedList<ProjectMember>(projectMember, projectMemberPage, 10);
            PaginatedList<Account> _groupMembers = new PaginatedList<Account>(groupMember, accountPage, 10);

            totalProjectMemberPage = _projectMembers.TotalPages;
            totalGroupAccountPage = _groupMembers.TotalPages;

            projectMemberPageIndex = _projectMembers.PageIndex;
            groupMembersPageIndex = _groupMembers.PageIndex;

            int numOfProjectMember = _projectMembers.Count;
            int numOfGroupMember = _groupMembers.Count;
            ProjectMembers = new List<MemberAccount>();
            GroupMembers = new List<MemberAccount>();

            for (int i = 0; i < numOfProjectMember; i++)
            {
                ProjectMembers.Add(new MemberAccount 
                {
                    AccountId = _projectMembers[i].AccountId.ToString(),
                    UserName = _projectMembers[i].Account.UserName,
                    FirstName = _projectMembers[i].Account.Profiles.FirstOrDefault().FirstName,
                    LastName = _projectMembers[i].Account.Profiles.FirstOrDefault().LastName,
                    Email = _projectMembers[i].Account.Profiles.FirstOrDefault().Email,
                    IsAssigned = _projectMembers[i].IsAssigned
                });
            }

            for (int i = 0; i < numOfGroupMember; i++)
            {
                GroupMembers.Add(new MemberAccount
                {
                    AccountId = _groupMembers[i].AccountId.ToString(),
                    UserName = _groupMembers[i].UserName,
                    FirstName = _groupMembers[i].Profiles.FirstOrDefault().FirstName,
                    LastName = _groupMembers[i].Profiles.FirstOrDefault().LastName,
                    Email = _groupMembers[i].Profiles.FirstOrDefault().Email
                });
            }
        }

        public ProjectMemberModel(string ProjectId, string KeyWord, int projectMemberPage, int accountPage)
        {
            projectId = ProjectId;
            this.keyWord = KeyWord.ToLower();
            var projectMember = projectManager.GetProjectMembers(ProjectId).Where(pm => pm.Account.UserName.ToLower().Contains(keyWord) || pm.Account.Profiles.FirstOrDefault().FirstName.ToLower().Contains(keyWord) || pm.Account.Profiles.FirstOrDefault().LastName.ToLower().Contains(keyWord) || pm.Account.Profiles.FirstOrDefault().Email.ToLower().Contains(keyWord)).OrderBy(m => m.Account.UserName);
            var groupMember = projectManager.GetGroupMembersExceptProjectMembers(ProjectId).Where(gm => gm.UserName.ToLower().Contains(keyWord) || gm.Profiles.FirstOrDefault().FirstName.ToLower().Contains(keyWord) || gm.Profiles.FirstOrDefault().LastName.ToLower().Contains(keyWord) || gm.Profiles.FirstOrDefault().Email.ToLower().Contains(keyWord)).OrderBy(m => m.UserName);
            PaginatedList<ProjectMember> _projectMembers = new PaginatedList<ProjectMember>(projectMember, projectMemberPage, 10);
            PaginatedList<Account> _groupMembers = new PaginatedList<Account>(groupMember, accountPage, 10);

            totalProjectMemberPage = _projectMembers.TotalPages;
            totalGroupAccountPage = _groupMembers.TotalPages;

            projectMemberPageIndex = _projectMembers.PageIndex;
            groupMembersPageIndex = _groupMembers.PageIndex;

            int numOfProjectMember = _projectMembers.Count;
            int numOfGroupMember = _groupMembers.Count;
            ProjectMembers = new List<MemberAccount>();
            GroupMembers = new List<MemberAccount>();

            for (int i = 0; i < numOfProjectMember; i++)
            {
                ProjectMembers.Add(new MemberAccount
                {
                    AccountId = _projectMembers[i].AccountId.ToString(),
                    UserName = _projectMembers[i].Account.UserName,
                    FirstName = _projectMembers[i].Account.Profiles.FirstOrDefault().FirstName,
                    LastName = _projectMembers[i].Account.Profiles.FirstOrDefault().LastName,
                    Email = _projectMembers[i].Account.Profiles.FirstOrDefault().Email,
                    IsAssigned = _projectMembers[i].IsAssigned
                });
            }

            for (int i = 0; i < numOfGroupMember; i++)
            {
                GroupMembers.Add(new MemberAccount
                {
                    AccountId = _groupMembers[i].AccountId.ToString(),
                    UserName = _groupMembers[i].UserName,
                    FirstName = _groupMembers[i].Profiles.FirstOrDefault().FirstName,
                    LastName = _groupMembers[i].Profiles.FirstOrDefault().LastName,
                    Email = _groupMembers[i].Profiles.FirstOrDefault().Email
                });
            }
        }

        public bool hasNextProjectMemberPage
        {
            get { return projectMemberPageIndex < totalProjectMemberPage; }
        }

        public bool hasNextGroupMembersPage
        {
            get { return groupMembersPageIndex < totalGroupAccountPage; }
        }

        public bool hasPreviousProjectMemberPage
        {
            get { return projectMemberPageIndex > 1; }
        }

        public bool hasPreviousGroupMembersPage
        {
            get { return groupMembersPageIndex > 1; }
        }
    }

    public class MemberAccount
    {
        public string AccountId { get; set; }
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public bool IsAssigned { get; set; }
    }
}